package com.sxfq.loansupermarket.backsystem.modular.basicData.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PayServiceInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchPayServiceInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.VipLevelInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.ImemberPayServiceService;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.DepartmentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 科室管理
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/memberPayService")
public class memberPayServiceController extends BaseController {
    @Autowired
    ImemberPayServiceService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;

    private String PREFIX = "/basicData/payService/";

    /**
     * 跳转到科室管理页面
     */
    @RequestMapping("")
    public String payService(Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getUser().getDepartmentList() == null) {
            String getListSql = "select a.department as department from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_grade_info",0) + " a where a.is_upper=1 group by a.department order by a.id;";
            List<DepartmentInfo> departmentList=sqlMapper.selectList(getListSql,DepartmentInfo.class);
            ShiroKit.getUser().setDepartmentList(departmentList);
        }
        model.addAttribute("departmentList",ShiroKit.getUser().getDepartmentList());
        return PREFIX + "memberPayService.html";
    }

    @RequestMapping("/memberPayServicelist")
    @ResponseBody
    public Object testrInfoList(SearchPayServiceInfo info, HttpServletRequest request) {
        Page<PayServiceInfo> page = new PageFactory<PayServiceInfo>().defaultPage();
        try {
            List<PayServiceInfo> list = service.selectPageListByQuery(page, info);
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     * 会员免费项目设置
     */
    @RequestMapping(value = "/freeService/{id}")
    public String freeService(@PathVariable String id, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        String productList = "select * from "+ redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"vip_level_info",0) +" order by num ";
        List<VipLevelInfo> vipLevelInfoList = sqlMapper.selectList(productList, VipLevelInfo.class);
        model.addAttribute("payServiceId", id);
        model.addAttribute("vipLevelInfoList", vipLevelInfoList);
        return PREFIX + "to_freeService.html";
    }

    //添加会员对应次数
    @RequestMapping(value = "/addPayServiceVip", method = RequestMethod.POST)
    @ResponseBody
    public int addPayServiceVip(String payServiceId,String[] operativeList, HttpServletRequest request) {
        String vipLevelId = "";
        Integer number;
        String sql1="delete from "+redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_vip",0)+" where pay_service_id='"+payServiceId+"'";
        sqlMapper.delete(sql1);
        for (int i = 0; i < operativeList.length; i++) {
            vipLevelId = operativeList[i].split("#")[0];
            number = Integer.parseInt(operativeList[i].split("#")[1]);
            for (int j = 0; j < number; j++) {
                String sql="insert into "+redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_vip",0)+" (vipid,pay_service_id) " +
                        "value ('"+vipLevelId+"','"+payServiceId+"')";
                sqlMapper.insert(sql);
            }
        }
        return 1;
    }
}